package dao.impl;

import bean.dto.RoleQueryPage;
import bean.dto.RolesDTO;
import bean.pojo.Roles;
import bean.vo.RoleVo;
import commons.jdbc.basic.JDBCUtil;
import dao.RoleDao;

import java.util.*;

/**
 * dao.impl
 * User: mcw
 * Date: 2024/12/16 上午10:09
 * motto:   春天（spring）没有虫子（bug)
 * Description:
 * Version: V1.0
 */
public class RoleDaoImpl implements RoleDao {
    /**
     * 查询记录数
     * @param roleQueryPage
     * @return
     */
    @Override
    public Long findByTotal(RoleQueryPage roleQueryPage) {
        //sql
        StringBuffer   sql=new StringBuffer("select  count(*) ct  from  roles  where  1=1 ");
        //集合参数
        List<Object> parameters=new ArrayList<>();
        //验证是否为空  null  "    "
        if(roleQueryPage.getRoleName()!=null){
            parameters.add(roleQueryPage.getRoleName());
            sql.append(" and  role_name like concat('%',?,'%') ");
        }
        System.out.println("sql:"+sql);
        System.out.println("参数:"+parameters);
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql.toString(),parameters.toArray());
        //默认0条
        Long  ct=0L;
        if(query.size()>0){
            //该变
            ct=(Long)query.get(0).get("ct");
        }
        return ct;
    }

    /**
     * 分页查询
     * @param roleQueryPage
     * @return
     */
    @Override
    public List<Roles> findByPage(RoleQueryPage roleQueryPage) {
        //sql
        StringBuffer   sql=new StringBuffer("select * from roles  where 1=1 ");
        //集合参数
        List<Object>  parameters=new ArrayList<>();
        //验证是否为空  null  "    "
        if(roleQueryPage.getRoleName()!=null){
            parameters.add(roleQueryPage.getRoleName());
            sql.append(" and  role_name like concat('%',?,'%') ");
        }
        sql.append("  order by  role_id ");
        sql.append("  limit  "+((roleQueryPage.getPage()-1)*roleQueryPage.getLimit())
                +","+roleQueryPage.getLimit());
        System.out.println("sql:"+sql);
        System.out.println("参数:"+parameters);
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql.toString(), parameters.toArray());
        System.out.println(Arrays.toString(query.toArray()));
        if(query.size()>0){
            List<Roles> list=new ArrayList<>();
            for (Map<String, Object> row : query) {//Community
                Roles  roles=new Roles();
                roles.setRoleId((Integer) row.get("role_id"));
                roles.setRoleName((String) row.get("role_name"));
                roles.setDescription((String) row.get("description"));
                list.add(roles);
            }
            System.out.println("List<Roles>:list:"+list);
            return  list;
        }
        return null;
    }
    /**
     * 添加权限信息
     * @param roles
     * @return
     */
    @Override
    public boolean insert(RolesDTO roles) {
        //先往角色表添加角色
        JDBCUtil jdbcUtil=new JDBCUtil();
        String sql="insert into roles(role_name,description) values(?,?)";
        int update = jdbcUtil.update(sql, roles.getRoleName(), roles.getDescription());
        //查询出刚添加的角色id
        String sql1="select role_id from roles where role_name=?";
        List<Map<String, Object>> query = jdbcUtil.query(sql1, roles.getRoleName());
        Integer roleId= (Integer) query.get(0).get("role_id");
        //遍历
        String sql2="insert into role_permissions(role_id,permission_id) values(?,?)";
        List<Integer> permissionIdList = roles.getPermissionIdList();
        int update2=0;
        if(permissionIdList.size()>0){
            for(int i=0;i<permissionIdList.size();i++){
                Integer permissionId=permissionIdList.get(i);
                update2 = jdbcUtil.update(sql2, roleId, permissionId);
            }
        }else {
            update2=1;
        }


        System.out.println("sql:"+sql);
        return update > 0&& update2 > 0;
    }

    /**
     * 根据id删除权限信息
     * @param roleId
     * @return
     */
    @Override
    public boolean deleteById(Integer roleId) {
        String sql1="delete from role_permissions where role_id=?";
        String sql2="delete from roles where role_id=?";
        JDBCUtil jdbcUtil=new JDBCUtil();
        int update = jdbcUtil.update(sql1, roleId);
        int update2 = jdbcUtil.update(sql2, roleId);
        return update>0;
    }

    /**
     * 修改权限信息
     * @param roles
     * @return
     */
    @Override
    public boolean update(RolesDTO roles) {
        JDBCUtil jdbcUtil=new JDBCUtil();
        String roleName = roles.getRoleName();
        String description = roles.getDescription();
        Integer roleId = roles.getRoleId();
        List<Integer> permissionIdList = roles.getPermissionIdList();
        //先删除角色原有权限
        String sql="delete from role_permissions where role_id=?";
        int update = jdbcUtil.update(sql, roleId);
        //再添加角色权限
        String sql1="update roles set role_name=?,description=? where role_id=?";
        int update1 = jdbcUtil.update(sql1, roleName, description, roleId);
        String sql2="insert into role_permissions(role_id,permission_id) values(?,?)";

        int update2=0;
        for(int i=0;i<permissionIdList.size();i++){
            Integer permissionId=permissionIdList.get(i);
            update2 = jdbcUtil.update(sql2, roleId, permissionId);
        }

        System.out.println("参数:"+roles);
        System.out.println("sql:"+sql);
        System.out.println("sql1:"+sql1);
        System.out.println("sql2:"+sql2);
        return update1>0 && update2>0;
    }

    @Override
    public List<RoleVo> findAllList() {
        String sql="select role_id,role_name from roles";
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql);
        List<RoleVo> list=new ArrayList<>();
        for (Map<String, Object> row : query) {
            RoleVo roleVo=new RoleVo();
            roleVo.setRoleId((Integer) row.get("role_id"));
            roleVo.setRoleName((String) row.get("role_name"));
            list.add(roleVo);
        }
        System.out.println("List<RoleVo> findAllList():sql:"+sql);
        System.out.println("List<RoleVo> findAllList():list:"+list);
        return list;
    }

}
